﻿using System;

namespace BitButterECS.DataType
{
	public abstract class ComponentState
	{
		public void Reset()
		{
			IsUpdated = false;
		}

		public bool IsUpdated
		{
			get; protected set;
		}
	}

	public class ComponentState<TState> : ComponentState
	{
		public ComponentState(TState value)
		{
			stateValue = value;
			IsUpdated = false;
		}

		public TState Value
		{
			get => stateValue;
			set
			{
				if (!IsUpdated)
				{
					stateValue = value;
					IsUpdated = true;
				}
			}
		}
		TState stateValue;
	}

	public static class ComponentStateExtension
	{
		public static bool IsComponentState(this object obj)
		{
			return obj.GetType().IsComponentState();
		}

		public static bool IsComponentState(this Type fieldType)
		{
			Type type = fieldType;
			while (type != null)
			{
				if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ComponentState<>))
				{
					return true;
				}
				type = type.BaseType;
			}
			return false;
		}
	}
}
